package br.com.poc.pitagorasnumero.view.adapter;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import br.com.poc.pitagorasnumero.R;
import br.com.poc.pitagorasnumero.presenter.IFacebookSharePresenter;
import br.com.poc.pitagorasnumero.view.dto.ResultPitagorasDTO;

import com.google.inject.Inject;

/**
 * The Class PitagorasDiagramAdapter.
 * 
 * @author irlan.gomes
 */
public class PitagorasDetailPagerAdapter extends PagerAdapter implements
		OnClickListener {

	/** The inflater. */
	@Inject
	private LayoutInflater inflater;

	/** The result list. */
	private List<ResultPitagorasDTO> resultList = new ArrayList<ResultPitagorasDTO>();

	/** The result dto. */
	private ResultPitagorasDTO resultDTO;

	/** The facebook share. */
	@Inject
	private IFacebookSharePresenter facebookShare;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.view.PagerAdapter#getCount()
	 */
	@Override
	public int getCount() {
		return resultList.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.view.PagerAdapter#isViewFromObject(android.view.View,
	 * java.lang.Object)
	 */
	@Override
	public boolean isViewFromObject(final View view, final Object object) {
		return (view == object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.view.PagerAdapter#destroyItem(android.view.ViewGroup,
	 * int, java.lang.Object)
	 */
	@Override
	public void destroyItem(final ViewGroup container, final int position,
			final Object view) {
		if (container instanceof ViewPager && view instanceof View) {
			((ViewPager) container).removeView((View) view);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.view.PagerAdapter#instantiateItem(android.view.ViewGroup
	 * , int)
	 */
	@Override
	public Object instantiateItem(final ViewGroup container, final int position) {

		View view = container;
		ViewHolder holder;

		if (view.getTag() == null) {
			view = inflater.inflate(R.layout.expandable_list, null);
			holder = builderHolder(view);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		prepareHolder(position, holder);

		if (container instanceof ViewPager) {
			container.addView(view);
		}

		return view;
	}

	/**
	 * Prepare holder.
	 * 
	 * @param position
	 *            the position
	 * @param holder
	 *            the holder
	 */
	private void prepareHolder(final int position, final ViewHolder holder) {
		resultDTO = resultList.get(position);
		final ExpandableListAdapter expandableAdapter = new ExpandableListAdapter(
				inflater, resultDTO);
		holder.btnFacebook.setOnClickListener(this);
		holder.expandableListView.setAdapter(expandableAdapter);
	}

	public void setResult(final List<ResultPitagorasDTO> resultado) {
		this.resultList = resultado;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(final View v) {
		if (v.getId() == R.id.btnFace) {
			facebookShare.initFacebook(v.getContext());
			if (this.facebookShare.isFacebookLoggedIn()) {
				final String shared = MessageFormat.format("{0}\n\n\n {1}",
						resultDTO.getTypeSignification(),
						resultDTO.getSignificationName());
				this.facebookShare.shareFacebook(shared);
			}
		}

	}

	/**
	 * Builder holder.
	 * 
	 * @param view
	 *            the view
	 * @return the view holder
	 */
	private ViewHolder builderHolder(final View view) {
		final ViewHolder holder = new ViewHolder();
		holder.btnFacebook = (Button) view.findViewById(R.id.btnFace);
		holder.expandableListView = (ExpandableListView) view
				.findViewById(R.id.expandableListView1);
		return holder;
	}

	/**
	 * The Class ViewHolder.
	 */
	public static class ViewHolder {

		/** The btn facebook. */
		private Button btnFacebook;

		/** The expandable list view. */
		private ExpandableListView expandableListView;

	}

}
